package woooo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class AccountSystem {
    private static JLabel remainingLabel;
    private static JLabel expenseLabel;
    private static JLabel daysLeftLabel;
    private static JLabel todayExpenseLabel;
    private static TextArea detailsTextArea;
    private static StringBuilder detailsBuilder;
    static boolean flag = false;


    Calendar calendar = Calendar.getInstance();                             // 获取当前日期
    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);                   //今天是这个月的第几天
    int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);    //现在月份的最大天数
    int daysLeft = daysInMonth - currentDay;                               // 计算距离本月末的天数


    public static void main(String[] args) {

        AccountSystem accountSystem = new AccountSystem();
        accountSystem.FirstWindow();
    }

    public void FirstWindow() {

        // 创建第一个窗口
        JFrame firstFrame = new JFrame("记账系统");
        firstFrame.setBounds(500,200,100,600);
        firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //顶层面板和底层面板
        JPanel topPanel = new JPanel(new GridLayout(2, 2));
        JPanel bottomPanel = new JPanel();


        // 创建四个label
        remainingLabel = new JLabel("本月剩余：0");
        remainingLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        daysLeftLabel = new JLabel("距离本月末天数："+daysLeft);
        daysLeftLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        expenseLabel = new JLabel("本月消费：0");
        expenseLabel.setFont(new Font("宋体", Font.PLAIN, 30));
        todayExpenseLabel = new JLabel("今日消费金额：0");
        todayExpenseLabel.setFont(new Font("宋体", Font.PLAIN, 30));


        //按钮“记”
        JButton recordButton = new JButton("记");
        recordButton.setPreferredSize(new Dimension(120, 120));
        String text = recordButton.getText(); // 获取按钮上的文本
        recordButton.setFont(new Font("宋体", Font.PLAIN, 40));
        recordButton.setText(text);

        recordButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                SecondWindow();
            }
        });

        firstFrame.setLayout(new BorderLayout());

        // 将组件添加到主窗口
        topPanel.add(remainingLabel);
        topPanel.add(daysLeftLabel);
        topPanel.add(expenseLabel);
        topPanel.add(todayExpenseLabel);
        bottomPanel.add(recordButton);

        // 将顶层面板和底层面板添加到边界布局中
        firstFrame.add(topPanel, BorderLayout.CENTER);
        firstFrame.add(bottomPanel, BorderLayout.SOUTH);

        // 显示主窗口
        firstFrame.pack();
        firstFrame.setSize(600,300);
        firstFrame.setVisible(true);
    }
    private static void SecondWindow(){

        //创建第二个窗口
        JFrame secondFrame = new JFrame("记账");
        secondFrame.setBounds(10,500,500,500);

        JPanel Panel = new JPanel(new GridLayout(1, 2));

        //创建两个按钮
        JButton incomeButton = new JButton("收入");
        incomeButton.setPreferredSize(new Dimension(120, 70));
        String text1 = incomeButton.getText(); // 获取按钮上的文本
        incomeButton.setFont(new Font("宋体", Font.PLAIN, 20));
        incomeButton.setText(text1);

        incomeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Third1Window();
            }
        });

        JButton expenseButton = new JButton("支出");
        expenseButton.setPreferredSize(new Dimension(120, 70));
        String text2 = expenseButton.getText(); // 获取按钮上的文本
        expenseButton.setFont(new Font("宋体", Font.PLAIN, 20));
        expenseButton.setText(text2);

        expenseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                Third2Window();
            }
        });

        Panel.add(incomeButton);
        Panel.add(expenseButton);

        secondFrame.add(Panel,BorderLayout.CENTER);

        // 显示第二个窗口
        secondFrame.pack();
        secondFrame.setSize(600,300);
        secondFrame.setVisible(true);



    }
    private static void Third1Window() {

        // 创建第三个窗口
        JFrame Third1Frame = new JFrame("收入");
        Third1Frame.setBounds(450,300,500,500);
        Third1Frame.setLayout(new BorderLayout());
        Third1Frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel topPanel1 = new JPanel(new GridLayout(2, 1));
        JPanel bottomPanel1 = new JPanel();

        // 创建组件
        JLabel incomeAmountLabel = new JLabel("收入金额：");
        incomeAmountLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        JTextField incomeAmountField = new JTextField();

        JLabel incomeReasonLabel = new JLabel("收入原因：");
        incomeReasonLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        JTextField incomeReasonField = new JTextField();



        JButton nextButton = new JButton("下一步");
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 更新主窗口数据
                double incomeAmount = Double.parseDouble(incomeAmountField.getText());

                updateMainWindowData1(incomeAmount);

                // 打开第三个窗口
                if(!flag) {
                    FourthWindow();
                    flag = true;
                }
                updateFourth(incomeAmount,incomeReasonField.getText());


                // 关闭第二个窗口
                Third1Frame.dispose();
            }
        });

        // 将组件添加到第二个窗口
        topPanel1.add(incomeAmountLabel);
        topPanel1.add(incomeAmountField);
        topPanel1.add(incomeReasonLabel);
        topPanel1.add(incomeReasonField);

        bottomPanel1.add(nextButton);

        Third1Frame.add(topPanel1,BorderLayout.CENTER);
        Third1Frame.add(bottomPanel1,BorderLayout.SOUTH);

        // 显示第二个窗口
        Third1Frame.pack();
        Third1Frame.setSize(600,300);
        Third1Frame.setVisible(true);
    }

    private static void Third2Window() {

        // 创建第三个窗口
        JFrame Third2Frame = new JFrame("记账");
        Third2Frame.setBounds(450,300,500,500);
        Third2Frame.setLayout(new BorderLayout());
        Third2Frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel topPanel1 = new JPanel(new GridLayout(2, 1));
        JPanel bottomPanel1 = new JPanel();


        // 创建组件

        JLabel expenseAmountLabel = new JLabel("支出金额：");
        expenseAmountLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        JTextField expenseAmountField = new JTextField();

        JLabel expenseReasonLabel = new JLabel("支出原因：");
        expenseReasonLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        JTextField expenseReasonField = new JTextField();

        JButton nextButton = new JButton("下一步");
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 更新主窗口数据
                double expenseAmount = Double.parseDouble(expenseAmountField.getText());
                updateMainWindowData2(expenseAmount);

                // 打开第四个窗口
                if(!flag) {
                    FourthWindow();
                    flag = true;
                }
                updateFourth(expenseAmount,expenseReasonField.getText());


                // 关闭第二个窗口
                Third2Frame.dispose();
            }
        });

        // 将组件添加到第二个窗口

        topPanel1.add(expenseAmountLabel);
        topPanel1.add(expenseAmountField);
        topPanel1.add(expenseReasonLabel);
        topPanel1.add(expenseReasonField);
        bottomPanel1.add(nextButton);

        Third2Frame.add(topPanel1,BorderLayout.CENTER);
        Third2Frame.add(bottomPanel1,BorderLayout.SOUTH);

        // 显示第二个窗口
        Third2Frame.pack();
        Third2Frame.setSize(600,300);
        Third2Frame.setVisible(true);
    }

    private static void FourthWindow() {
        // 创建第四个窗口
        JFrame fourthFrame = new JFrame("收支明细");
        fourthFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        fourthFrame.setLayout(new BorderLayout());

        // 创建组件
        detailsTextArea = new TextArea();
        detailsTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(detailsTextArea);

        // 拼接收支明细内容
        detailsBuilder = new StringBuilder();
        detailsBuilder.append("日期\t收入/支出\t金额\t\t\t\t收入/支出原因\n");

        // 将组件添加到第四个窗口
        fourthFrame.add(scrollPane, BorderLayout.CENTER);

        // 显示第四个窗口
        fourthFrame.setSize(500, 400);
        fourthFrame.setVisible(true);
    }

    private static void updateFourth(double Amount,String s){

        // 这里用示例数据代替实际的每日明细记录

        Calendar calendar = Calendar.getInstance();
        if(Amount > 0)
            detailsBuilder.append(calendar.get(Calendar.DAY_OF_MONTH)).append("日\t").append("  收入  \t\t").append(Amount).append("\t\t\t\t").append(s).append("\n");
        if(Amount < 0)
            detailsBuilder.append(calendar.get(Calendar.DAY_OF_MONTH)).append("日\t").append("  支出  \t\t").append(Amount).append("\t\t\t\t").append(s).append("\n");

        detailsTextArea.setText(detailsBuilder.toString());

    }

    private static void updateMainWindowData1(double incomeAmount) {
        // 获取当前主窗口数据
        double remaining = Double.parseDouble(remainingLabel.getText().substring(5));
        int daysLeft = Integer.parseInt(daysLeftLabel.getText().substring(8));


        // 更新数据
        remaining += incomeAmount;


        // 更新主窗口显示
        remainingLabel.setText("本月剩余：" + remaining);
        daysLeftLabel.setText("距离本月末天数：" + daysLeft);
    }

    private static void updateMainWindowData2(double expenseAmount) {
        // 获取当前主窗口数据
        double remaining = Double.parseDouble(remainingLabel.getText().substring(5));
        double expense = Double.parseDouble(expenseLabel.getText().substring(5));
        int daysLeft = Integer.parseInt(daysLeftLabel.getText().substring(8));
        double todayExpense = Double.parseDouble(todayExpenseLabel.getText().substring(7));

        // 更新数据
        remaining += expenseAmount;
        expense += expenseAmount;
        expense = -expense;
        todayExpense += expenseAmount;
        todayExpense = -todayExpense;


        // 更新主窗口显示
        remainingLabel.setText("本月剩余：" + remaining);
        expenseLabel.setText("本月消费：" + expense);
        daysLeftLabel.setText("距离本月末天数：" + daysLeft);
        todayExpenseLabel.setText("今日消费金额：" + todayExpense);
    }

}

